package cn.czh0123.controller;

import cn.czh0123.domain.Wallpaper;
import cn.czh0123.dto.WallpaperDTO;
import cn.czh0123.enums.WallpaperOperateType;
import cn.czh0123.exception.BusinessException;
import cn.czh0123.msg.CommonCodeMsg;
import cn.czh0123.msg.WallpaperCodeMsg;
import cn.czh0123.query.WallpaperCategoryQuery;
import cn.czh0123.query.WallpaperQuery;
import cn.czh0123.redis.service.IWallpaperOperateRedisService;
import cn.czh0123.service.IWallpaperService;
import cn.czh0123.utils.ResultWrapper;
import cn.czh0123.vo.WallpaperVO;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* 壁纸控制层
*/
@RestController
@RequestMapping("wallpapers")
public class WallpaperController {

    @Resource
    private IWallpaperService wallpaperService;
    @Resource
    private IWallpaperOperateRedisService wallpaperOperateRedisService;

    @GetMapping("/list")
    public ResultWrapper<IPage<Wallpaper>> list(WallpaperQuery qo){
        return ResultWrapper.success(wallpaperService.queryPage(qo));
    }

    @GetMapping("/getWallpaperDetail")
    public ResultWrapper<WallpaperVO> getWallpaperDetail(Long id){
        if (id == null) {
            return ResultWrapper.error(CommonCodeMsg.PARAM_INVALID);
        }
        return ResultWrapper.success(wallpaperService.getWallpaperDetail(id));
    }

    @GetMapping("/carousels")
    public ResultWrapper<List<Wallpaper>> carousels() {
        return ResultWrapper.success(wallpaperService.queryCarousel());
    }

    @SaCheckLogin
    @PostMapping("/operate")
    public ResultWrapper<?> operate(Long wallpaperId, Integer type) {
        if (wallpaperId == null || type == null) {
            throw new BusinessException(CommonCodeMsg.PARAM_INVALID);
        }
        wallpaperOperateRedisService.operate(wallpaperId, WallpaperOperateType.fromCode(type));
        return ResultWrapper.success();
    }

    @SaCheckLogin
    @PostMapping("/save")
    public ResultWrapper<?> save(@Validated WallpaperDTO wallpaper) {
        if (wallpaper.getFile() == null) {
            return ResultWrapper.error(WallpaperCodeMsg.UPLOAD_EMPTY);
        }
        wallpaperService.saveWallpaper(wallpaper);
        return ResultWrapper.success();
    }

    @GetMapping("/category")
    public ResultWrapper<?> category(WallpaperCategoryQuery query) {
        return ResultWrapper.success(wallpaperService.getCategory(query));
    }
}
